(function() {

    angular.module('cf-content')
    
    .controller('InfoModelCtrl', InfoModelCtrl)

    .controller('ReviewImgModelCtrl', ReviewImgModelCtrl)

    .controller('EnsureModelCtrl', EnsureModelCtrl)

    .controller('SelectModelCtrl', SelectModelCtrl)

    .controller('TextModelCtrl', TextModelCtrl)

    EnsureModelCtrl.$inject = ['$scope', '$uibModalInstance', '$timeout', '$filter', 'item'];

    function EnsureModelCtrl($scope, $uibModalInstance, $timeout, $filter, item) {

        $scope.item = item;

        $scope.ok = function() {
            $uibModalInstance.close(true);
        };

        $scope.cancel = function() {
            $uibModalInstance.dismiss(false);
        };

    }

    ReviewImgModelCtrl.$inject = ['$scope', '$uibModalInstance', '$timeout', '$filter', 'path'];

    function ReviewImgModelCtrl($scope, $uibModalInstance, $timeout, $filter, path) {

        $timeout(function() {
            if (path.indexOf('http://7xrjgr.com1.z0.glb.clouddn.com') < 0)
                $scope.path = $filter('AuthFilter')(path);
            else
                $scope.path = path;
        }, 0, true)
        $scope.cancel = function() {
            $uibModalInstance.dismiss(false);
        };

    }

    InfoModelCtrl.$inject = ['$scope', '$uibModalInstance', '$timeout', 'info', 'type'];

    function InfoModelCtrl($scope, $uibModalInstance, $timeout, info, type) {

        $scope.type = type ? type : 'info';
        $scope.info = info;
        $timeout(function() {
            $uibModalInstance.close(true);
        }, 1500, true)
    }

    SelectModelCtrl.$inject = ['$scope', '$uibModalInstance', '$timeout', 'Api', 'field'];

    function SelectModelCtrl($scope, $uibModalInstance, $timeout, Api, field) {
        $scope.field = {
            firstFieldList: [],
            firstField: '',
            secondFieldList: [],
            secondField: '',
            thridFieldList: [],
            thridField: '',
            fourFieldList: [],
            fourField: ''
        };
        $scope.field = {
            name: field.field_name,
            id: field.field_id
        }
        initData()

        function initData() {
            Api.Field.Query().$promise.then(function(data) {
                $scope.field.firstFieldList = data.data;
                $scope.field.firstField = $scope.field.firstFieldList[0] ? $scope.field.firstFieldList[0].id : '';
                if ($scope.field.firstField) {
                    $scope.initSecondData()
                }
            })
        }
        $scope.initSecondData = function() {
            if ($scope.field.firstField)
                Api.Field.Query({
                    parent_field_id: $scope.field.firstField
                }).$promise.then(function(data) {
                    $scope.field.secondFieldList = data.data;
                    $scope.field.secondField = $scope.field.secondFieldList[0] ? $scope.field.secondFieldList[0].id : '';
                })
        }
        $scope.ok = function() {
            if ($scope.field.secondField) {
                for (var i in $scope.field.secondFieldList) {
                    if ($scope.field.secondFieldList[i].id == $scope.field.secondField) {
                        $uibModalInstance.close($scope.field.secondFieldList[i]);
                        break;
                    }
                }
            }

        };

        $scope.cancel = function() {
            $uibModalInstance.dismiss(false);
        };
    }

    TextModelCtrl.$inject = ['$scope', '$uibModalInstance', '$timeout', 'Api'];

    function TextModelCtrl($scope, $uibModalInstance, $timeout, Api) {
        $scope.ok = function() {
            if ($scope.info)
                $uibModalInstance.close($scope.info);
            else
                $scope.hasError = false;
        };

        $scope.cancel = function() {
            $uibModalInstance.dismiss(false);
        };
    }

})();